<?

/** Doc printing class.
 *  Note! This uses soPdf and therefore requires pdflib support php.
 *
 *  This collection of functions (libary) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *
 *  @author Jyry Kuukkanen
 *
 *  $Id: dzdocprint.php,v 1.8 2005/04/10 17:11:53 jyry Exp $
 */

include_once("sosqlhi.php");
include_once("sodatetime.php");
include_once("soset.php");
include_once("soinifiles.php");
include_once("sostring.php");
include_once("sodebug.php");
include_once("dzpdfprint.php");
include_once("dzgeneric.php");
include_once("dzstruct.php");
include_once("dzkone.php");
include_once("dzconst.php");


/** Document printing class.
 *  Reads a set from database and produces PDF output for it.
 *  @var string $__DbAlias Database name to connect to
 *  @var dzPrint $__Printer Printing object
 *  @var string $__StructIniStr Struct ini string to kone
 */
class dzDocPrint {
    var $__DbAlias;
    var $__Printer;
    var $__StructIniStr;

    /** Constructor
     *  @param string $DbAlias Db alias name
     *  @param string $Template Template soStream source
     *  @param string $MainScriptPath When called by a script from the commend
     *      line, this paramter must be passed to pass the working directory
     */
    function dzDocPrint($DbAlias, $Template, $Language, $MainScriptPath = "") {
        include("sodebug.inc");

        if ($MainScriptPath) {
            $_SERVER["SCRIPT_FILENAME"] = $MainScriptPath."/dzdocprint.php";
        };
        $this->__DbAlias = $DbAlias;

        $this->__Printer = new dzPdfPrint($Template, $Language);
        $this->__makeStruct();
    } // constructor


    /** Builds struct object from template source
     */
    function __makeStruct() {
        $template = $this->__Printer->_getTemplate();

        $this->__StructIniStr =
            $template->toString(array(DZ_TABLES_INISECTION), "\n").
                "\n[".DZ_COLUMNS_INISECTION."]\n";
        $cols = array_merge($this->__Printer->getAreaColNames(DZPA_HEAD, 1),
                            $this->__Printer->getAreaColNames(DZPA_FOOT, 1),
                            $this->__Printer->getAreaColNames(DZPA_DETAILS, 1),
                            $this->__Printer->getAreaColNames(DZPA_TOTALS, 1)
                            );

        foreach ($cols as $col_name) {
            $this->__StructIniStr .= $col_name."=\n";
        };

        $this->__Struct = new dzStruct();
        $this->__Struct->setup($this->__StructIniStr);
    } // __makeStruct


    /** Prints out a set
     *  @param array/any $Key Key to set to print.
     *  @param string $Target Stream string where to write output to.
     *  @param bool $Simulate When set(1), simulates printing instead of doing
     *      it.
     *  @return bool Returns true(1) on success or false(0) on error.
     */
    function printOut($KeyVals, $Target, $Simulate = 0) {
        include("sodebug.inc");
        $request = new dzItu();

        /* Set the session id for locks and user information to access database
         */
        $request->setBlockItem(DZI_CONTROL, DZK_SESSION, "nosession");
        $request->setBlockItem(DZI_CONTROL, DZK_USER, "dzdocprint");
        $request->setBlockItem(DZI_CONTROL, DZK_OWNER, "dzdocprint");
        $request->setBlockItem(DZI_CONTROL, DZK_DATABASE, $this->__DbAlias);
        $request->setBlockItem(DZI_CONTROL, DZK_STRUCT,
                               $this->__StructIniStr);
        $request->setBlockItem(DZI_CONTROL, DZK_REQUEST, DZK_REQ_READ);

        /* Insert the key of the requested data as payload */
        $master_table = $this->__Struct->getMasterTableName();
        $key_cols = $this->__Struct->getKeyCols($master_table);
        if (count($key_cols) != count($KeyVals)) return 0; // <-- EXIT

        $key = new soArraySet();
        $i = 0;
        $icount = count($key_cols);
        $keys = array_keys($KeyVals);
        for ($i = 0; $i < $icount; $i++) {
            $key->setArrayItem($master_table.'.'.$key_cols[$i], 0,
                               $KeyVals[$keys[$i]]);
        };
        $request->setBlock(DZI_DATA, $key);

        /* Finally send the request */
        $result_itu = sendRpcToKone($request);

        /* check for rpc error */
        if (isRpcError($result_itu)) return 0; // <-- EXIT

        /* Get the result code */
        $kone_result = $result_itu->getBlockItemAt(DZI_CONTROL, DZK_RESULT, 0);
        /* if result ok get the payload and store it */
        if ($kone_result > 0) {
            $this->__Printer->beginDoc();

            sodebug("dzDocPrint: kone read request ok", SOD_DET);
            $this->__Printer->printOut($result_itu->getBlock(DZI_DATA),
                                       $Simulate);

            $this->__Printer->commitDoc($Target);
            $result = 1;
        } else {
            sodebug("dzDocPrint: kone resulted an error", SOD_DET);
            $result = 0;
        }; // if result

        return $result;
    } // printOut

}; // dzDocPrint

?>
